package vn.allmobile.ambnews.activities.main;

import java.util.List;
import java.util.Vector;

import vn.allmobile.ambnews.R;
import vn.allmobile.ambnews.activities.list.AllPapersAdapter;
import vn.allmobile.ambnews.fragment.PagerAdapter;
import vn.allmobile.ambnews.fragment.allpapers_sort_fragment;
import vn.allmobile.ambnews.fragment.allpapers_treeview_fragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;

public class MainAllPapersActivity extends FragmentActivity {
	AllPapersAdapter mAdapter;
	ViewPager mViewPaper;
	private PagerAdapter mPagerAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_allpapers);
		initPage();
		setTab();
	}

	private void initPage() {
		List<Fragment> fragments = new Vector<Fragment>();
		fragments.add(Fragment.instantiate(this,
				allpapers_treeview_fragment.class.getName()));
		fragments.add(Fragment.instantiate(this,
				allpapers_sort_fragment.class.getName()));
		this.mPagerAdapter = new PagerAdapter(
				super.getSupportFragmentManager(), fragments);

		mViewPaper = (ViewPager) super.findViewById(R.id.allpaper_viewpager);
		mViewPaper.setAdapter(this.mPagerAdapter);
	}

	private void setTab() {
		mViewPaper.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageScrollStateChanged(int position) {
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
			}

			@Override
			public void onPageSelected(int position) {
				
				// TODO Auto-generated method stub
				switch (position) {
				case 0:
					findViewById(R.id.first_tab).setVisibility(View.VISIBLE);
					findViewById(R.id.second_tab).setVisibility(View.INVISIBLE);
					break;

				case 1:
					findViewById(R.id.first_tab).setVisibility(View.INVISIBLE);
					findViewById(R.id.second_tab).setVisibility(View.VISIBLE);
					break;
				}
			}

		});

	}

	public void datachanged() {
		//initPage();
		mPagerAdapter.notifyDataSetChanged();
	}
}
